/*global $, plusplus:true, alert, clearInterval: false, clearTimeout: false, document: false, event: false, frames: false, history: false, Image: false, location: false, name: false, navigator: false, Option: false, parent: false, screen: false, setInterval: false, setTimeout: false, window: false, XMLHttpRequest: false */
 function gup(name) {
"use strict";
    var regexS = "", regex, results;
    name = name.replace(/[\[]/, "[").replace(/[\]]/, "]");
    regexS = "[\\?&]" + name + "=([^&#]*)";
    regex = new RegExp(regexS);
    results = regex.exec(window.location.href);
    if (results === null) {
        return "";
    }  
    else {
         return results[1];
    } 
}

 function submitTheForm(formId) {
	 "use strict";
	 var submitForm = document.getElementById(formId);
	 submitForm.submit();
 }
 
// Write on clockId DOM-element TimeLeft string with style
 function updateClockElement(clockId, data, clrStyle) {
    "use strict";
    var element = document.getElementById(clockId);
    element.style.color = clrStyle;
    element.innerHTML = data; // show time left on element
 }

 // function stringTimeLeft returns formatted string in hours, minutes and secs.  
 function stringTimeLeft(secondsLeft) {
	"use strict";
	var secsLeftMinusHour, hoursLeft = 0, formatTimeLeft = "";
	secsLeftMinusHour = 0; // seconds left after substraction from the hour(s).
	hoursLeft = Math.floor(secondsLeft / 3600); // calculate if more than 1
	// we got more than 1 hour
	if (hoursLeft > 0) {
        secsLeftMinusHour = secondsLeft - (hoursLeft * 3600);
	} 
	else {
        secsLeftMinusHour = secondsLeft;
	}
	
	if (secsLeftMinusHour >= 60) {
		if (secsLeftMinusHour % 60 > 0) {
			if ((secondsLeft / 60) <= 5) {
				formatTimeLeft = Math.floor(secsLeftMinusHour / 60).toString()
						+ " min. " + (secsLeftMinusHour % 60).toString()
						+ " sec.";
			} 
			else { 
				formatTimeLeft = Math.round(secsLeftMinusHour / 60).toString() + " min.";
			}
		}
		else {
			if ((secsLeftMinusHour / 60) === 60) {
				formatTimeLeft = "59 min.";
		   }
		   else {
			  formatTimeLeft = (secsLeftMinusHour / 60).toString() + " min.";
		   }  
		}
	} 
	else { 
		formatTimeLeft = secsLeftMinusHour.toString() + " sec.";
	}
	
	if (hoursLeft > 0) {
		formatTimeLeft = hoursLeft.toString() + " hrs. " + formatTimeLeft;
	}
	
	return formatTimeLeft; // string to represent time left
 }

// For user experience
 function UpdateTimerConstructor(secondsLeft, clockId, submitFormId, timeDelta) {
	 "use strict";
	 var updateTimer = function() {
		     // check every 3 secs how many time is left.
		     // var timeDelta = 3, colorStyle = "green";
		     var colorStyle = "green";
		  
		     secondsLeft = secondsLeft - timeDelta;
		     // make element content red last 2 minutes, else green
		     colorStyle = "green";
		     if (secondsLeft >= 0) {
		         if (secondsLeft <= 120) {
		             colorStyle = "red";
		         } 
		         // Write TimeLeft on element clockId with a formatted string and color-style
		         updateClockElement(clockId, stringTimeLeft(secondsLeft), colorStyle);

		         // call the function again
		         setTimeout(function () {updateTimer(); }, 1000 * timeDelta);
		     } 
		     else { // Time is up
		         submitTheForm(submitFormId);
		  	}
		  };
	updateTimer();
 }
 
 
/* set exam info for examinee */
function setExamInfo(new_style_json) {
"use strict";
    var elName, elDuration, strDuration = "";
    elName = document.getElementById("ExamName");
    elName.innerHTML = new_style_json.header.toString();
    elDuration = document.getElementById("ExamDuration");
    strDuration = new_style_json.duration.toString() + " min.";
    elDuration.innerHTML = strDuration;
}

function drawQuestion(question, qIndex) {
"use strict";	
    var i = 0, text, form, title, alt, br;
    form = document.createElement("div");
    form.setAttribute('id', "form" + qIndex.toString());
    title = document.createElement("h3");
    title.innerHTML = question.question;
    form.appendChild(title);
    // Add attibutes of question object
    for (i = 0; i < question.alternatives.length; i = i + 1) {
        alt = document.createElement("input");
        alt.setAttribute('type', "radio");
        alt.setAttribute('name', "gruppe" + qIndex.toString());
        alt.setAttribute('id', "gruppe" + qIndex.toString() + "_" + i.toString());
        alt.setAttribute('value',  question.alternatives[i]);	// LATEST
        text = document.createTextNode(question.alternatives[i]);
        form.appendChild(alt);
        form.appendChild(text);
        br = document.createElement("br");
       form.appendChild(br);
    }
    return form;
}


function drawQuestions(new_style_json) {
"use strict";	
    var question_list, qElement, place, iDurationSecs = 0, i = 0,  timeDelta = 3, fu;

    setExamInfo(new_style_json);
		
    question_list = new_style_json.qList;
    for (i = 0; i < question_list.length; i = i + 1) {
        qElement = drawQuestion(question_list[i], i);
        place = document.getElementById("questionlist"); // questionlist is
															// DIV ID in html
															// body to place
															// questions
        place.appendChild(qElement);
    }

    iDurationSecs = new_style_json.duration * 60;
    // var frmSubmit = document.getElementById("frmExamData");
    fu = new  UpdateTimerConstructor(iDurationSecs, "Time_Left", "frmExamData", timeDelta);	// Set time left for  user experience
}

function init(examId, examinationId) {
"use strict";
    var elExamId, elExaminationId;
    $.getJSON('/json?examId=' + examId, drawQuestions);

    elExamId = document.getElementById("examId");
    elExamId.value = examId;

    elExaminationId = document.getElementById("examinationId");
    elExaminationId.value = examinationId;
}


